package com.ewhale.feitengguest.ui.user.message;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.MessageDetailDto;
import com.ewhale.feitengguest.dto.MessageDto;
import com.ewhale.feitengguest.presenter.user.message.NewsBulletinPresenter;
import com.ewhale.feitengguest.ui.WebViewActivity;
import com.ewhale.feitengguest.ui.user.message.adapter.NewsBulletinAdapter;
import com.ewhale.feitengguest.view.user.message.NewsBulletinView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.orhanobut.hawk.Hawk;
import com.simga.library.activity.MBaseFragment;
import com.simga.library.adapter.recyclerview.OnItemListener;
import com.simga.library.utils.Dp2PxUtil;
import com.simga.library.utils.HawkKey;
import com.simga.library.widget.CoustomRefreshView;
import com.simga.library.widget.RecycleViewDivider;
import com.youth.xframe.widget.loadingview.XLoadingView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class NewsBulletinFragment extends MBaseFragment<NewsBulletinPresenter> implements NewsBulletinView {
    @BindView(R.id.list)
    RecyclerView mList;
    @BindView(R.id.ref_layout)
    CoustomRefreshView mRefLayout;
    @BindView(R.id.xloading_view)
    XLoadingView xloadingView;

    private List<MessageDto> datas;
    private NewsBulletinAdapter mBulletinAdapter;
    private int pageNum =1;
    @Override
    protected int getViewId() {
        return R.layout.fragment_base_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        requestList(pageNum);
    }
    @Override
    protected void firstInit(Bundle savedInstanceState) {
        datas = new ArrayList<>();
        mList.setLayoutManager(new LinearLayoutManager(mContext));
        mList.setHasFixedSize(true);
        mList.addItemDecoration(new RecycleViewDivider(mContext,
                Dp2PxUtil.dip2px(mContext, 10)));
        mBulletinAdapter = new NewsBulletinAdapter(datas);
        mList.setAdapter(mBulletinAdapter);
    }

    @Override
    protected void initListener() {
        mBulletinAdapter.setOnItemClickListener(new OnItemListener() {
            @Override
            public void onItem(View view, int position) {
                presenter.loadMessageDetail(datas.get(position).getMessageId());
            }
        });
        mRefLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNum = 1;
                presenter.loadMessageList(pageNum);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                pageNum++;
                presenter.loadMessageList(pageNum);
            }
        });
        xloadingView.setOnRetryClickListener(v -> {
            pageNum = 1;
            xloadingView.showLoading();
            presenter.loadMessageList(pageNum);
        });
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }


    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
        if(postCode == 1){
            xloadingView.showError();
        }
        onLoad(-1);
    }


    public void onLoad(int size) {
        if (mRefLayout == null) {
            return;
        }
        mRefLayout.finishRefreshLoadingMore();
        if (size < 10) {
            mRefLayout.enableLoadMore(false);
        } else {
            mRefLayout.enableLoadMore(true);
        }
    }

    @Override
    public void showMessageList(List<MessageDto> result, int pageNum) {
        if(pageNum == 1){
            datas.clear();
        }
        datas.addAll(result);
        mBulletinAdapter.notifyDataSetChanged();
        if(datas.size() == 0){
            xloadingView.showEmpty();
        }else{
            xloadingView.showContent();
        }
        onLoad(result.size());
    }

    @Override
    public void showMessageDetail(MessageDetailDto detailDto) {
        WebViewActivity.open(mContext,detailDto.getTitle(),detailDto.getContent());
    }

    /**
     * 请求消息列表
     * @param pageNum
     */
    public void requestList(int pageNum){
        if (!Hawk.get(HawkKey.IS_USER_LOGIN, false)) {
            getActivity().sendBroadcast(new Intent(HawkKey.USER_LOGIN_OUT));
            return;
        }
        this.pageNum = pageNum;
        xloadingView.showLoading();
        presenter.loadMessageList(pageNum);
    }
}
